package com.dotstone.chipism.adapter;

import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Key;
import com.dotstone.chipism.util.KeyManager;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

public class KeyGridVewAdapter extends BaseAdapter {

	private Context context;
	private List<Key> data;
	private int imageResId;
	private int stringResId;

	public KeyGridVewAdapter(Context context) {
		this.context = context;
	}

	public KeyGridVewAdapter(Context context, List<Key> data) {
		this.data = data;
		this.context = context;
	}

	public void setData(List<Key> data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.item_more_key,
					null);
			holdView = new HoldView();
			holdView.btn = (Button) view.findViewById(R.id.key_btn);
			view.setTag(holdView);
		} else {
			holdView = (HoldView) view.getTag();
		}
		final Key k = data.get(position);
		holdView.btn.setTag(k.getKeyNum());
		int content = KeyManager.getInstance().getContentTypeByKeyNum(
				k.getKeyNum());
		if (content == 0) {
			imageResId = KeyManager.getInstance().getDrawableResId(
					k.getKeyNum());
			holdView.btn.setBackgroundResource(imageResId);
		} else {
			stringResId = KeyManager.getInstance()
					.getStringResId(k.getKeyNum());
			holdView.btn.setText(stringResId);
		}
		holdView.btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.i("wmy", "点击了"+k.getKeyNum());
			}
		});

		return view;
	}

	HoldView holdView;

	class HoldView {
		private Button btn;
	}

}
